package com.moweilin.mo_gateway;

import com.beust.jcommander.Parameter;
import com.beust.jcommander.Parameters;
import com.moweilin.mo_gateway.spi.impl.DefaultConfigProvider;
import io.vertx.core.DeploymentOptions;
import io.vertx.core.Verticle;
import io.vertx.core.Vertx;
import io.vertx.core.json.JsonObject;
import org.apache.commons.lang3.NotImplementedException;

/**
 * Created by moweilin on 2017/2/20.
 */
@Parameters(commandNames = {"start"},commandDescription = "start gateway application")
public class StartCommand {
    public static final String DEBUG_KEY= "debug";
    private boolean cluster;
    @Parameter(names = "-debug",descriptionKey = "debugDescription")
    private Boolean debug = false;


    public Boolean getDebug() {
        return debug;
    }

    public void setDebug(Boolean debug) {
        this.debug = debug;
    }

    public void startServer(){
        if(cluster) {
            startCluster();
        }else {
            startSingleton();
        }
    }
    protected void startCluster(){
        throw new NotImplementedException("no implement startCluster()");
    }
    protected void startSingleton(){
        Vertx vertx = Vertx.vertx();
        DeploymentOptions deploymentOptions = new DeploymentOptions();
        JsonObject config = new JsonObject();
        config.put(DEBUG_KEY,debug);
        deploymentOptions.setConfig(config);
        deploymentOptions.getConfig().put(DEBUG_KEY,debug);
        Class<? extends Verticle> serverClass = DefaultConfigProvider.getInstance().getServerClass();
        vertx.deployVerticle(serverClass.getName(),deploymentOptions);
    }
}
